// pages/finance/bills/index/index.js
var http = require('../../../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tableHeader: [
      "时间", "账单类型", "可用余额", "操作"
    ],
    totalPage: 0,
    form: {
      page: 1,
      size: 15,
      state: '',
      bill_date: '',
    },
    tableList: []
  },
  //获取表单，并对表单进行处理后进行查找
  search(e) {
    this.setData({
      "form.page": e.detail.state
    });
    this.setData({
      "tableList": []
    });
    this.setData({
      "form.state": e.detail.state
    });
    if (e.detail.start_date === '开始日期' || e.detail.end_date === '结束日期') {

    } else {
      this.setData({
        "form.bill_date": e.detail.start_date + "|" + e.detail.end_date
      });
    }
    this.getBillList();
  },
  //获取待入赃账单
  getBillList() {
    let that = this;
    let list = this.data.tableList;
    wx.showLoading({
      title: 'loading',
    });
    if (!that.data.form.state) {
      delete this.data.form.state;//如果该属性不存在，则删除该属性
    } 
    http.GET({
      url: 'api/order/billList',
      data: that.data.form,
      success: function(res) {
        wx.hideLoading();
        if(res.data.error===0) {
          that.setData({
            'totalPage': res.data.data.last_page
          });
          let newList = res.data.data.data;
          for (let index = 0; index < newList.length; index++) {
            list.push(newList[index]);
          }
          that.setData({
            "tableList": list
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getBillList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.form.page < this.data.totalPage) {
      let page = ++this.data.form.page;
      this.setData({
        "form.page": page
      });
      this.getBillList();
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})